package com.gamex.findit.controller.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.gamex.findit.Constants;
import com.gamex.findit.internal.DefaultConfig;
import com.gamex.findit.model.Character;
import com.gamex.findit.service.CharacterService;
import com.gamex.findit.service.CommonCodeService;
import com.gamex.findit.service.MemberItemService;
import com.gamex.findit.util.CommonUtil;
import com.gamex.findit.util.Generator;

@Controller("adminCharacter")
public class CharacterController {
	
	private static final Logger logger = LoggerFactory.getLogger(CharacterController.class);
	
	@Autowired CharacterService characterService;
	@Autowired MemberItemService memberItemService;
	@Autowired CommonCodeService commonCodeService;
	@Autowired DefaultConfig config;
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param character
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/characterList")
	public String CharacterList( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, Model model
							, @ModelAttribute Character character) throws Exception{

		int count = characterService.getCharacterCount(character);
		List<com.gamex.findit.model.Character> result = characterService.getCharacter(character);
		model.addAttribute("list", result);
		model.addAttribute("count", count);
		model.addAttribute("pageNo", character.getPageNo());
		model.addAttribute("pageGroup", character.getPageGroup());
		model.addAttribute("searchParam", character);
	return "/character/characterList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param character
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/characterView")
	public String viewGameItem( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, ModelMap model
							, @ModelAttribute Character character) throws Exception{
		
		model.addAttribute("view", characterService.getCharacterView(character.getCharId()));
		model.addAttribute("searchParam", character);
	return "/character/characterView";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param file
	 * @param character
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/addCharacter")
	public String addCharacter( HttpSession session
						   , HttpServletRequest request
						   , HttpServletResponse response
						   , Model model
						   , @RequestParam("file") MultipartFile file
						   , @ModelAttribute Character character) throws Exception{
		
		String name = Generator.generateFileName();
		if(file.getSize() > 0){
			CommonUtil.writeImage(request, file, Constants.DEFAULT_CHAR_IMG_PATH, name);
			character.setImg(String.format(Constants.DEFAULT_CHAR_URL, config.getHostAddress()) + name);
		}
		
		characterService.insertCharacter(character);
		
	return "redirect:/admin/characterList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param file
	 * @param character
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/updateCharacter.*")
	public String updateCharacter( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @RequestParam("file") MultipartFile file
							  , @ModelAttribute Character character) throws Exception{
		
		String name = Generator.generateFileName();
		if(file.getSize() > 0){
			CommonUtil.writeImage(request, file, Constants.DEFAULT_CHAR_IMG_PATH, name);
			character.setImg(String.format(Constants.DEFAULT_CHAR_URL, config.getHostAddress()) + name);
		}
		
		characterService.updateCharacter(character);
		
	return "redirect:/admin/characterList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param character
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/deleteCharacter")
	public String deleteCharacter( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @ModelAttribute Character character) throws Exception{
		
		characterService.deleteCharacter(character.getCharId());
		
	return "redirect:/admin/characterList";
	}
	
}
